package com.ungao.tools.generator.create;


import com.ungao.tools.generator.beans.TableInfo;
import com.ungao.tools.generator.builder.TableBuilder;
import com.ungao.tools.generator.connection.DbConnection;
import com.ungao.tools.generator.converter.CaseConverter;
import com.ungao.tools.generator.engine.FreemarkerTemplateEngine;
import com.ungao.tools.generator.engine.TemplateEngine;
import com.ungao.tools.generator.engine.config.TemplateConfig;
import com.ungao.tools.generator.engine.type.EngineType;
import com.ungao.tools.generator.rules.MysqlColumnType;
import com.ungao.tools.generator.util.PathUtil;

import java.sql.Connection;
import java.util.List;

public class DefaultMultipleCreator extends AbstractCreator<List<TableInfo>, List<String>> {

    public DefaultMultipleCreator(List<String> names) {
        super(names);
    }

    public DefaultMultipleCreator(List<String> names, List<String> alias) {
        super(names, alias);
    }

    @Override
    public DefaultMultipleCreator connection(Connection connection) throws Exception {
        super.tables = new TableBuilder(super.names, super.alias)
                .create(new DbConnection(connection))
                .handle(MysqlColumnType.getInstance())
                .converter(new CaseConverter())
                .build();
        return this;
    }

    @Override
    public DefaultMultipleCreator connection(String url, String username, String password, String driver) throws Exception {
        super.tables = new TableBuilder(super.names, super.alias)
                .create(new DbConnection(url, username, password, driver))
                .handle(MysqlColumnType.getInstance())
                .converter(new CaseConverter())
                .build();
        return this;
    }

    @Override
    public DefaultMultipleCreator configuration(TemplateConfig configBuilder) throws Exception {
        super.configuration(configBuilder);
        return this;
    }

    @Override
    public void write(EngineType engineType) throws Exception {
        if (EngineType.Freemarker.equals(engineType)) {
            TemplateEngine engine = new FreemarkerTemplateEngine().init(super.configBuilder);
            for (TableInfo info : tables) {
                if (configBuilder.getModulePath() != null && !configBuilder.getModulePath().equals("")) {
                    final String modulePath = PathUtil.convertPath(configBuilder.getModulePath());
                    String targetPath = PathUtil.concatToPath(PathUtil.createPath(modulePath), info.getClassName(), configBuilder.getModuleSuffix(), PathUtil.JAVA);
                    engine.writer(info, "Model.ftl", targetPath);
                }
                if (configBuilder.getMapperPath() != null && !configBuilder.getMapperPath().equals("")) {
                    final String mapperPath = PathUtil.convertPath(configBuilder.getMapperPath());
                    String targetPath = PathUtil.concatToPath(PathUtil.createPath(mapperPath), info.getClassName(), configBuilder.getMapperSuffix(), PathUtil.JAVA);
                    engine.writer(info, "Mapper.ftl", targetPath);
                }
                if (configBuilder.getXmlPath() != null && !configBuilder.getXmlPath().equals("")) {
                    final String xmlPath = PathUtil.convertPath(configBuilder.getXmlPath());
                    String targetPath = PathUtil.concatToPath(PathUtil.createPath(xmlPath), info.getClassName(), configBuilder.getXmlSuffix(), PathUtil.XML);
                    engine.writer(info, "Xml.ftl", targetPath);
                }
                if (configBuilder.getServicePath() != null && !configBuilder.getServicePath().equals("")) {
                    final String servicePath = PathUtil.convertPath(configBuilder.getServicePath());
                    String targetPath = PathUtil.concatToPath(PathUtil.createPath(servicePath), info.getClassName(), configBuilder.getServiceSuffix(), PathUtil.JAVA);
                    engine.writer(info, "Service.ftl", targetPath);
                }
                if (configBuilder.getImplPath() != null && !configBuilder.getImplPath().equals("")) {
                    final String implPath = PathUtil.convertPath(configBuilder.getImplPath());
                    String targetPath = PathUtil.concatToPath(PathUtil.createPath(implPath), info.getClassName(), configBuilder.getServiceSuffix(), configBuilder.getImplSuffix(), PathUtil.JAVA);
                    engine.writer(info, "ServiceImpl.ftl", targetPath);
                }
                if (configBuilder.getControlPath() != null && !configBuilder.getControlPath().equals("")) {
                    final String controlPath = PathUtil.convertPath(configBuilder.getControlPath());
                    String targetPath = PathUtil.concatToPath(PathUtil.createPath(controlPath), info.getClassName(), configBuilder.getControlSuffix(), PathUtil.JAVA);
                    engine.writer(info, "Controller.ftl", targetPath);
                }
            }
        }
    }
}
